Convert a List of Tuples into a DictionaryΒΆ
for a, b in LOT:D.setdefault(a, []).append(b)
Convert a List of Tuples into a Dictionary.
# create a list
LOT = [("x", 1), ("x", 2), ("x", 3), ("y", 1), ("y", 2), ("z", 1)]
D = {}
for a, b in LOT:
D.setdefault(a, []).append(b)
print(D) # {'y': [1, 2], 'z': [1], 'x': [1, 2, 3]}